#include "raylib.h"
#include "raymath.h"
#include <cstdio>


int main(void)
{
    // 初始化窗口
    const int screenWidth = 1010;
    const int screenHeight = 1380;
    InitWindow(screenWidth, screenHeight, "raylib - 着色器多边形遮罩");
    
    // 加载纹理
    Image checkedImage = GenImageChecked(1010, 1010, 32, 32, RED, BLUE);
    Texture2D texture = LoadTexture("../assets/imgs/zou.png");
    // Texture2D texture = LoadTextureFromImage(checkedImage);
    
    // 创建着色器
    Shader shader = LoadShader(0,"D:\\zxionf\\vscode\\maze\\maze-game\\build\\assets\\shaders\\mask.fs");
    
    // 定义多边形顶点（示例：五角星）
    Vector2 polygonPoints[10];
    int pointCount = 10;
    
    // 初始化五角星顶点
    float centerX = screenWidth / 2.0f;
    float centerY = screenHeight / 2.0f;
    float outerRadius = 150.0f;
    float innerRadius = 60.0f;
    
    for (int i = 0; i < pointCount; i++) {
        float angle = i * 2.0f * PI / pointCount - PI / 2.0f;
        float radius = (i % 2 == 0) ? outerRadius : innerRadius;
        polygonPoints[i].x = centerX + cosf(angle) * radius;
        polygonPoints[i].y = centerY + sinf(angle) * radius;
    }
    
    // 获取着色器uniform位置
    int resolutionLoc = GetShaderLocation(shader, "resolution");
    int pointCountLoc = GetShaderLocation(shader, "pointCount");
    
    // 设置分辨率
    // float resolution[2] = { (float)screenWidth, (float)screenHeight };
    float resolution[2] = { (float)300, (float)300 };
    SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
    
    // 设置顶点数量
    SetShaderValue(shader, pointCountLoc, &pointCount, SHADER_UNIFORM_INT);

    Camera2D camera;
    camera.target = {0,0};
        camera.offset = {0,0};
        camera.rotation = 0.0f;
        camera.zoom = 0.5f;
    
    // 主循环
    SetTargetFPS(60);
    
    while (!WindowShouldClose())
    {
        ClearBackground(WHITE);
        // 更新逻辑
        // 动态更新五角星顶点（旋转效果）
        float time = GetTime();
        for (int i = 0; i < pointCount; i++) {
            float angle = i * 2.0f * PI / pointCount - PI / 2.0f + time * 0.5f;
            float radius = (i % 2 == 0) ? outerRadius : innerRadius;
            polygonPoints[i].x = centerX + cosf(angle) * radius;
            polygonPoints[i].y = centerY + sinf(angle) * radius;
        }
        
        // 开始绘制到渲染纹理
        // BeginTextureMode(target);
        // ClearBackground(BLANK); // 透明背景
        
        // 绘制原始纹理（作为测试）
       
        DrawTexture(texture,0,0,WHITE);
        
        // EndTextureMode();
        
        // 开始主绘制
        BeginDrawing();
        // ClearBackground(DARKGRAY);
        
        // 应用着色器并绘制渲染纹理 
        BeginMode2D(camera);
        BeginShaderMode(shader);
        Vector2 p = GetMousePosition();
        SetShaderValue(shader, resolutionLoc, &p, SHADER_UNIFORM_VEC2);
        // 更新顶点数据到着色器
        for (int i = 0; i < pointCount; i++) {
            char pointName[32];
            sprintf(pointName, "points[%d]", i);
            int pointLoc = GetShaderLocation(shader, pointName);
            float pointValue[2] = { polygonPoints[i].x, polygonPoints[i].y };
            SetShaderValue(shader, pointLoc, pointValue, SHADER_UNIFORM_VEC2);
        }
        
        // 绘制应用了着色器的渲染纹理
        DrawTextureRec(texture, 
                      (Rectangle){ 0, 0, (float)texture.width, (float)texture.height }, 
                      (Vector2){ 0, 0 }, 
                      WHITE);
        
        EndShaderMode();
        
        // 绘制多边形轮廓（可选，用于可视化）
        for (int i = 0; i < pointCount; i++) {
            int next = (i + 1) % pointCount;
            DrawLineV(polygonPoints[i], polygonPoints[next], GREEN);
        }
        
        // 绘制顶点（可选，用于可视化）
        for (int i = 0; i < pointCount; i++) {
            DrawCircleV(polygonPoints[i], 5, RED);
        }
        
        // 绘制说明文字
        DrawText("多边形内部: 显示原始纹理", 10, 10, 20, WHITE);
        DrawText("多边形外部: 黑色", 10, 40, 20, WHITE);
        DrawText("红色圆点: 多边形顶点", 10, 70, 20, WHITE);
       
        EndMode2D();
        EndDrawing(); 
        
    }
    
    // 清理资源
    UnloadShader(shader);
    UnloadTexture(texture);
    CloseWindow();
    
    return 0;
}